unit grammar DDL ;
    	rule TOP {
        :my  @*col-names;
	    'CREATE' 'TABLE' <table-name> \( <column-section> [\, <constraint-section> ]? \) 
        #{say @*col-names}
        }


    token table-name { \S+ }

    rule column-section { <column>+ %  ','  }
    #rule endblock {\, <constraint-section>}
    rule constraint-section { <table-constraint>+  % ','  }


    token comma { ',' }

    rule column {
        <column-name> <column-type>  [\( <size> [ \, <precision>]? \)]? <column-constraint>*
        <?{
           @*col-names.append($/<column-name>.Str) if $/<column-name>;
          #say $/<column-name>
         
          if $/<column-type> ~~
                | 'INT'
                | 'INTEGER'
                | 'TINYINT'
                | 'SMALLINT'
                | 'MEDIUMINT'
                | 'BIGINT'
                | 'INSIGNED BIG INT'
                | 'INT2'
                | 'INT8'
                | 'TEXT'
                | 'CLOB'
                | 'REAL'
                | 'DOUBLE'
                | 'DOUBLE PRECISION'
                | 'FLOAT'
                | 'BLOB'
                | 'NUMERIC'
                | 'BOOLEAN'
                | 'DATE'
                | 'DATETIME' { $/<size> ~~ Nil && $/<precision> ~~ Nil }
        elsif $/<column-type> ~~
                | 'CHARACTER'
                | 'CHAR'
                | 'VARCHAR'
                | 'VARYING CHARACTER'
                | 'NCHAR'
                | 'NVARCHAR'
                | 'NATIVE CHARACTER' { $/<size> && $/<precision> ~~ Nil }
        elsif $/<column-type> ~~ 'DECIMAL' { $/<size> && $/<precision>}
        else { False }
    }>
}
token column-type {
          | 'INT'
          | 'INTEGER'
          | 'TINYINT'
          | 'SMALLINT'
          | 'MEDIUMINT'
          | 'BIGINT'
          | 'INSIGNED BIG INT'
          | 'INT2'
          | 'INT8'
          | 'TEXT'
          | 'CLOB'
          | 'REAL'
          | 'DOUBLE'
          | 'DOUBLE PRECISION'
          | 'FLOAT'
          | 'BLOB'
          | 'NUMERIC'
          | 'BOOLEAN'
          | 'DATE'
          | 'DATETIME'
          | 'CHARACTER'
          | 'CHAR'
          | 'VARCHAR'
          | 'VARYING CHARACTER'
          | 'NCHAR'
          | 'NVARCHAR'
          | 'NATIVE' 'CHARACTER'
          | 'DECIMAL'
}
		  
        
    
token column-name { \w+ }




token size { \d+ }

token precision { \d+ }

proto rule column-constraint {*}
rule column-constraint:sym<primary-key> {
    'PRIMARY KEY'
    }
rule column-constraint:sym<default> {
    'DEFAULT' \( <defualt-value> \)
    }
token default-value { \w+ }
rule column-constraint:sym<not-null> {
    'NOT NULL'
    }
rule column-constraint:sym<unique> { 'UNIQUE'  }





rule table-constraint {
    'CONSTRAINT' <constraint-name> <constraint-type> \( <keys>  \)
    <?{  
        my $bool = True;
        for $/<keys><index-key>>>.Str -> $_ {
            unless $_ (elem) @*col-names.Seq { $bool = False}
        }
        $bool;

    }>
}
token constraint-name { \w+ }
token constraint-type {
    | 'PRIMARY  KEY' 
    | 'UNIQUE'
    | 'FOREIGN KEY'
}
rule keys {
    <index-key>+ % \,
    }
token index-key {\w+}

